package com.xjiuge.fruit.controller.login;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xjiuge.fruit.common.JsonResult;
import com.xjiuge.fruit.common.Response;
import com.xjiuge.fruit.common.ResponseCodeConstant;
import com.xjiuge.fruit.service.UserService;


@Controller("LoginController")
@RequestMapping("/login")
public class LoginController {

	public final static Logger LOG = Logger.getLogger(LoginController.class);
	
	@Resource(name="userService")
	private UserService userService;
	
	/**
	 * 前台登录
	 * LoginController.login<BR>
	 * <p>Author:xuhongcheng</p>
	 * <p>Date: 2017年10月30日  下午3:28:33</p>
	 * @param loginName
	 * @param loginPwd
	 * @param httpSession
	 * @return
	 */
	@RequestMapping(value = "/login")
	public String login(String loginName,String loginPwd,HttpSession httpSession){
		Response response = userService.login(loginName,loginPwd);
		if(!response.getCode().equals(ResponseCodeConstant.Common.success.getCode())){
			return "login";
		}else{
			httpSession.setAttribute("user", response.getData().get("user"));
		}
		return "index";
	}
	
	/**
	 * 后台管理界面登陆
	 * LoginController.admin_login<BR>
	 * <p>Author:xuhongcheng</p>
	 * <p>Date: 2017年10月30日  下午3:28:18</p>
	 * @param loginName
	 * @param loginPwd
	 * @param httpSession
	 * @return
	 */
	@RequestMapping(value = "/admin_login")
	@ResponseBody
	public JsonResult admin_login(String loginName,String loginPwd,HttpSession httpSession){
		Response response = userService.login(loginName,loginPwd);
		if(!response.getCode().equals(ResponseCodeConstant.Common.success.getCode())){
			return new JsonResult(response);
		}else{
			httpSession.setAttribute("user", response.getData().get("user"));
			return new JsonResult(response);
		}
	}
}
